// pages/detail/detail.js

import request from "../../utils/request.js";
import auth from "../../utils/auth.js";
import local from "../../utils/local";
import nav from "../../utils/nav.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
    appid: 'wx15a7538fa77ddfc0',
    isLocked: true,
    

    genCont: '',
    isAcc: '', // 0 未付款 1 已付款
    isAccGenCont: '',
    paiPanItemsCode: '',
    amo: '',
    juanCode: '',
    juanAmo: '',
    msAmo: '',
    settID: '',
    payAmo: '',
    transID: '',
    jndiName: '',
    nonceStr: '',
    notify_url: '',
    openId: '',
    package: '',
    prepay_id: '',
    sign: '',
    timeStamp: '',
    total_fee: '',
    nameData: {},
    sexData: {},
    animalData: {},
    astroData: {},
    ageData: {},
    address: '',
    addressData: {},
    shuData: {},
    loseData: {},
    tianYear: '',
    tianMonth: '',
    tianDay: '',
    tianHour: '',
    diYear: '',
    diMonth: '',
    diDay: '',
    diHour: '',

    tianYearColor: '',
    tianMonthColor: '',
    tianDayColor: '',
    tianHourColor: '',
    diYearColor: '',
    diMonthColor: '',
    diDayColor: '',
    diHourColor: '',


    tianFiveYear: '',
    tianFiveMonth: '',
    tianFiveDay: '',
    tianFiveHour: '',
    diFiveYear: '',
    diFiveMonth: '',
    diFiveDay: '',
    diFiveHour: '',

    tianFiveYearColor: '',
    tianFiveMonthColor: '',
    tianFiveDayColor: '',
    tianFiveHourColor: '',
    diFiveYearColor: '',
    diFiveMonthColor: '',
    diFiveDayColor: '',
    diFiveHourColor: '',
    MEMBER_PERM: '',
    PRICE_INFO: [],
    csDay: '',
    sex: '',
    show: false,
    selectedPlan: null,
    oneArr: [],
    twoArr: [],
    threeArr: [],
    fourArr: [],
    baZi: '',
  },

   /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const memberID = local.getLocal('memberID');
    let dataStr = decodeURIComponent(options.data)
    let params = JSON.parse(dataStr)
    const tempMEMBER_PERM = params.MEMBER_PERM
    const tempPAIPAN_INFO = params.PAIPAN_INFO
    const tempPRICE_INFO = params.PRICE_INFO
    let nameDataObject = {
      'title': '姓名', 
      'subtitle': tempPAIPAN_INFO.custName,
    }
    let sexDataObject = {
      'title': '性别', 
      'subtitle': tempPAIPAN_INFO.sex,
    }
    let baZiInfo = tempPAIPAN_INFO.baZiInfo
    let baZiInfoObject = JSON.parse(baZiInfo)
    console.log(baZiInfoObject)
    let baZiInfoResult = baZiInfoObject.result
    let animalDataObject = {
      'title': '生肖', 
      'subtitle': baZiInfoResult.Animal,
    }
    let astroDataObject = {
      'title': '星座', 
      'subtitle': baZiInfoResult.astro,
    }
    let tempAddress = tempPAIPAN_INFO.counName + tempPAIPAN_INFO.provName + tempPAIPAN_INFO.cityName + tempPAIPAN_INFO.areaName
    let addressDataObject = {
      'title': '地址', 
      'subtitle': tempAddress,
    }
    let ageStr = baZiInfoResult.year + '年' + baZiInfoResult.month + '月' + baZiInfoResult.day + '日' + tempPAIPAN_INFO.csHour + '时' + tempPAIPAN_INFO.csMinute + '分' + '(农历' + baZiInfoResult.ImonthCn + baZiInfoResult.IDayCn + ')'
    let ageDataObject = {
      'title': '生辰', 
      'subtitle': ageStr,
    }
    const eightAllObject = baZiInfoResult.eightAll
    const eightObject = eightAllObject.eight
    const eightOne = eightObject[0]
    const eightTwo = eightObject[1]
    const eightThree = eightObject[2]
    const eightFour = eightObject[3]
    const tempTianYear = eightOne.substring(0, 1);
    const tempTianYearColor = this.macthColor(tempTianYear)
    const tempTianMonth = eightTwo.substring(0, 1);
    const tempTianMonthColor = this.macthColor(tempTianMonth)
    const tempTianDay = eightThree.substring(0, 1);
    const tempTianDayColor = this.macthColor(tempTianDay)
    const tempTianHour = eightFour.substring(0, 1);
    const tempTianHourColor = this.macthColor(tempTianHour)
    const tempDiYear = eightOne.substring(1, 2);
    const tempDiYearColor = this.macthColor(tempDiYear)
    const tempDiMonth = eightTwo.substring(1, 2);
    const tempDiMonthColor = this.macthColor(tempDiMonth)
    const tempDiDay = eightThree.substring(1, 2);
    const tempDiDayColor = this.macthColor(tempDiDay)
    const tempDiHour = eightFour.substring(1, 2)
    const tempDiHourColor = this.macthColor(tempDiHour)
    let shuStr = eightAllObject.shu + '命'
    let shuDataObject = {
      'title': '命主', 
      'subtitle': shuStr,
    }
    const fiveAllObject = baZiInfoResult.fiveAll
    const fiveObject = fiveAllObject.five
    const fiveOne = fiveObject[0]
    const fiveTwo = fiveObject[1]
    const fiveThree = fiveObject[2]
    const fiveFour = fiveObject[3]
    const tempTianFiveYear = fiveOne.substring(0, 1);
    const tempTianFiveYearColor = this.macthFiveColor(tempTianFiveYear)

    const tempTianFiveMonth = fiveTwo.substring(0, 1);
    const tempTianFiveMonthColor = this.macthFiveColor(tempTianFiveMonth)
    const tempTianFiveDay = fiveThree.substring(0, 1);
    const tempTianFiveDayColor = this.macthFiveColor(tempTianFiveDay)
    const tempTianFiveHour = fiveFour.substring(0, 1);
    const tempTianFiveHourColor = this.macthFiveColor(tempTianFiveHour)

    const tempDiFiveYear = fiveOne.substring(1, 2);
    const tempDiFiveYearColor = this.macthFiveColor(tempDiFiveYear)
    const tempDiFiveMonth = fiveTwo.substring(1, 2);
    const tempDiFiveMonthColor = this.macthFiveColor(tempDiFiveMonth)
    const tempDiFiveDay = fiveThree.substring(1, 2);
    const tempDiFiveDayColor = this.macthFiveColor(tempDiFiveDay)
    const tempDiFiveHour = fiveFour.substring(1, 2)
    const tempDiFiveHourColor = this.macthFiveColor(tempDiFiveHour)
    let loseStr = ''
    if (fiveAllObject.lose.length == 0) {
      loseStr = '不缺'
    }else {
      loseStr = '缺' +  fiveAllObject.lose
    }
    
    let loseDataObject = {
      'title': '五行', 
      'subtitle': loseStr,
    }
    this.setData({
      memberID: memberID,
      genCont: tempPAIPAN_INFO.genCont,
      isAcc: tempPAIPAN_INFO.isAcc,
      paiPanItemsCode: tempPAIPAN_INFO.paiPanItemsCode,
      amo: tempPAIPAN_INFO.amo,
      juanCode: tempPAIPAN_INFO.juanCode,
      juanAmo: tempPAIPAN_INFO.juanAmo,
      msAmo: tempPAIPAN_INFO.msAmo,
      settID: tempPAIPAN_INFO.settID,
      payAmo: tempPAIPAN_INFO.payAmo,
      transID: tempPAIPAN_INFO.transID,
      jndiName: tempPAIPAN_INFO.jndiName,
      nameData: nameDataObject,
      sexData: sexDataObject,
      animalData: animalDataObject,
      astroData: astroDataObject,
      ageData: ageDataObject,
      address: tempAddress,
      addressData: addressDataObject,
      shuData: shuDataObject,
      loseData: loseDataObject,
      tianYear: tempTianYear,
      tianMonth: tempTianMonth,
      tianDay: tempTianDay,
      tianHour: tempTianHour,
      diYear: tempDiYear,
      diMonth: tempDiMonth,
      diDay: tempDiDay,
      diHour: tempDiHour,

      tianYearColor: tempTianYearColor,
      tianMonthColor: tempTianMonthColor,
      tianDayColor: tempTianDayColor,
      tianHourColor: tempTianHourColor,
      diYearColor: tempDiYearColor,
      diMonthColor: tempDiMonthColor,
      diDayColor: tempDiDayColor,
      diHourColor: tempDiHourColor,
      
      tianFiveYear: tempTianFiveYear,
      tianFiveMonth: tempTianFiveMonth,
      tianFiveDay: tempTianFiveDay,
      tianFiveHour: tempTianFiveHour,
      diFiveYear: tempDiFiveYear,
      diFiveMonth: tempDiFiveMonth,
      diFiveDay: tempDiFiveDay,
      diFiveHour: tempDiFiveHour,

      tianFiveYearColor: tempTianFiveYearColor,
      tianFiveMonthColor: tempTianFiveMonthColor,
      tianFiveDayColor: tempTianFiveDayColor,
      tianFiveHourColor: tempTianFiveHourColor,
      diFiveYearColor: tempDiFiveYearColor,
      diFiveMonthColor: tempDiFiveMonthColor,
      diFiveDayColor: tempDiFiveDayColor,
      diFiveHourColor: tempDiFiveHourColor,
      PRICE_INFO: tempPRICE_INFO,
      oneArr: tempPRICE_INFO['1天'],
      twoArr: tempPRICE_INFO['3天'],
      threeArr: tempPRICE_INFO['7天'],
      fourArr: tempPRICE_INFO['31天'],
      sex: tempPAIPAN_INFO.sex,
      baZi: tempPAIPAN_INFO.baZi,
    })

    let text = ''
    if (tempMEMBER_PERM == 1) {
      text = this.data.genCont
      this.setData({
        isLocked: false,
        isAccGenCont: text
      })
    }else {
      if (this.data.isAcc == 'false') {
        text =  this.data.genCont.slice(0, this.data.genCont.length * 30 / 100) + '...'
      }else {
        text = this.data.genCont
      }
  
      if (this.data.isAcc == 'true') {
        this.setData({
          isLocked: false,
        })
      }
      console.log(this.data.isLocked)
  
      this.setData({
        isAccGenCont: text
      })
    }
    
  },

  getPaipanitemsservicePay() {
    const that = this;
    let arr = this.data.prepay_id.split('prepay_id=')
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "303"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": this.data.paiPanItemsCode,
      "memberID": this.data.memberID,
      "amo": this.data.amo,
      "juanCode": this.data.juanCode,
      "juanAmo": this.data.juanAmo,
      "msAmo": this.data.msAmo,
      "settID": this.data.settID,
      "payAmo": this.data.total_fee,
      "transID": arr[1],
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log(dic)
    request.get('/paipanitemsservice',{param: dic}).then((res) => {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        that.setData({
          isLocked: false,
          isAccGenCont: that.data.genCont,
        })
      }
      
    })
  },

  getWxgetprecodeservlet(){
    console.log(this.data.selectedPlan.price)
    const that = this
    wx.login({
      success: (res) => {
        let dic = {
          appid: that.data.appid,//小程序APPID
          secret: "",//小程序密阴传空就行；
          appkey: "",//商户支付密阴传空就行；
          js_code: res.code,
          grant_type: 'authorization_code',//固定值
          wxUserInfo: 'intuit',//固定值
          total_fee: that.data.selectedPlan.price,//金额
          jndiName: that.data.jndiName,//paiPanItems.jndiName
          mch_id: "",//微信支付商户号，传空就行
          cardID: that.data.paiPanItemsCode //paiPanItems.paiPanItemsCode
        }
        console.log(dic)
        request.get('/wxgetprecodeservlet',dic).then((res) => {
          console.log(res)
          console.log(res.return)
          const returnCode = res.return
          if (returnCode == 1) {
            that.setData({
              nonceStr: res.nonceStr,
              notify_url: res.notify_url,
              openId: res.openId,
              package: res.package,
              prepay_id: res.prepay_id,
              sign: res.sign,
              timeStamp: res.timeStamp,
              total_fee: res.total_fee,
            })
            // console.log(that.data.nonceStr)
            // console.log(that.data.notify_url)
            // console.log(that.data.openId)
            // console.log(that.data.package)
            // console.log(that.data.prepay_id)
            // console.log(that.data.sign)
            // console.log(that.data.timeStamp)
            // console.log(that.data.total_fee)
            that.requestPaymentToWx()
          }
        })
      },
    })
    
  },

  macthColor(key) {
    if (key == '甲' || key == '乙' || key == '寅' || key == '卯') {
      return '#007700'
    }
    if (key == '丙' || key == '丁' || key == '巳' || key == '午') {
      return '#FF0000'
    }
    if (key == '戊' || key == '己' || key == '辰' || key == '戌' || key == '丑' || key == '未') {
      return '#972100'
    }
    if (key == '庚' || key == '辛' || key == '申' || key == '酉') {
      return '#FFC600'
    }
    if (key == '壬' || key == '癸' || key == '亥' || key == '子') {
      return '#131313'
    }
  },

  macthFiveColor(key) {
    if (key == '金') {
      return '#D9BB62'
    }
    if (key == '木') {
      return '#8D9F71'
    }
    if (key == '水') {
      return '#7EA9CA'
    }
    if (key == '火' ) {
      return '#D85063'
    }
    if (key == '土' ) {
      return '#A7856D'
    }
  },

  requestPaymentToWx(){
    console.log("requestPaymentToWx");
    const that = this
    wx.requestPayment({
      timeStamp: this.data.timeStamp,
      nonceStr: this.data.nonceStr,
      package: this.data.package,
      signType: 'MD5',
      paySign: this.data.sign,
      success (res) { 
        console.log(res)
        that.getPaipanitemsservicePay1()
      },
      fail (res) { }
    })
  },

  getPaipanitemsservicePay1() {
    let csDay = this.data.selectedPlan.days
    let sex = this.data.selectedPlan.days + '天会员'
    const that = this;
    let arr = this.data.prepay_id.split('prepay_id=')
    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "306"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": this.data.paiPanItemsCode,
      "memberID": this.data.memberID,
      "amo": this.data.selectedPlan.price,
      "juanCode": this.data.juanCode,
      "juanAmo": this.data.juanAmo,
      "msAmo": this.data.msAmo,
      "settID": this.data.settID,
      "payAmo": this.data.selectedPlan.price,
      "transID": arr[1],
      "csDay": csDay,
      "sex": sex,
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log(dic)
    request.get('/paipanitemsservice',{param: dic}).then((res) => {
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        that.setData({
          isLocked: false,
          isAccGenCont: that.data.genCont,
          show: false,
        })
      }
      
    })
  },

  // 点击去支付
  handleToPay(e) {
    this.setData({
      show: true,
    });
    // this.getWxgetprecodeservlet()
  },

  handlePay() {
    this.getWxgetprecodeservlet()
  },

  stopPropagation() {
    // 阻止事件冒泡
  },



  selectPlan(e) {
    console.log(e)
    const { days, price } = e.currentTarget.dataset;
    this.setData({
      selectedPlan: {
        days,
        price
      }
    });
    console.log(this.data.selectedPlan.days)
  },

  closeModal() {
    this.setData({
      show: false,
      selectedPlan: null,
    });
  },

  handleToShow() {
    this.getPaipanitemsservice2()
  },

  getPaipanitemsservice2() {
    const that = this;

    let userInfoDic = {
      "user": this.data.appid,
      "eventCode": "308"
    }
    let paiPanItemsStuctDic = {
      "paiPanItemsCode": this.data.paiPanItemsCode,
      "baZi": this.data.baZi,
      "memberID": this.data.memberID,
      "sex": this.data.sex
    }
    let dic = {
      userInfo: userInfoDic,
      paiPanItemsStuct: paiPanItemsStuctDic,
    }
    console.log("================")
    console.log(dic)
    request.get('/paipanitemsservice',{param: dic}).then((res) => {
      console.log("=========33333333=========")
      console.log(res)
      const returnCode = res.RETURN
      if (returnCode == 1) {
        let text = this.data.genCont + '<br>' + res.BAZI_INFO
        that.setData({
          genCont: text,
          isAccGenCont: text
        })
      }
    })
  },


 

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})